


function binarySearch( array,  target) {
    let count=0
    //查找范围起点
    let start = 0;
    //查找范围终点
    let end = array.length - 1;
    //查找范围中位数
    let mid;
    while (start <= end) {
        //mid=(start+end)/2 有可能溢出
        mid = start + Math.floor((end - start) / 2);
        count++
        if (array[mid] == target) {
            console.log('查找了'+count+'次');
            return mid;
        } else if (array[mid] < target) {
            start = mid + 1;
        } else {
            end = mid - 1;
        }
    }
    return -1;
} 

let array = new Array(1000);
for (let i = 0; i < 1000; i++) {
    array[i] = i;
}
console.log(binarySearch(array, 173));